package com.goodwill.androidtest.sort;

import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class SortTest {
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void main(String[] args) {
//        Integer[] arrays = new Integer[] {2,4, 1};
//        ISort sort = new SelectSort();
//        sort.sort(arrays);

//        testSort();

        int[] arrays = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int i = binarySearch(arrays, arrays.length, 50);
        System.out.println("find index: " + i);
    }

    private static void testSort() {
//        List<User> userList = new ArrayList<>();
//        userList.add(new User("张三", "23", 1));
//        userList.add(new User("李四", "2", 1));
//        userList.add(new User("王五", "3", 1));
//        userList.add(new User("赵六", "45", 1));
//        userList.add(new User("初七", "23", 2));
//        userList.add(new User("六九", "4", 2));
//
//        for (User user : userList) {
//            System.out.println(user);
//        }
//        System.out.println("----------------第一种方式 begin-------------");
//        List<User> collect = userList.stream().sorted(Comparator.comparing(User::getAge, new Comparator<String>() {
//            @Override
//            public int compare(String first, String second) {
////                if (TextUtils.isEmpty(first) || TextUtils.isEmpty(second)) return 0;
//
//                int one = Integer.valueOf(first);
//                int two = Integer.valueOf(second);
//                return one - two;
//            }
//        }).reversed()
//        .thenComparing(Comparator.comparing(User::getGrade)).reversed()
//        ).collect(Collectors.toList());
//
//        for (User user : collect) {
//            System.out.println(user);
//        }
//        System.out.println("----------------第一种方式 end-------------");
    }

    static int binarySearch(int[] array, int size, int value) {
        int lo = 0;
        int hi = size - 1;

        while (lo <= hi) {
            final int mid = (lo + hi) >>> 1;
            final int midVal = array[mid];

            System.out.println("mid:" + mid + " --- midVal:" + midVal);

            if (midVal < value) {
                lo = mid + 1;
            } else if (midVal > value) {
                hi = mid - 1;
            } else {
                return mid;  // value found
            }
        }

        return ~lo;  // value not present
    }
}
